package com.lfz.quality_effect.service.impl;

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.metadata.Sheet;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lfz.quality_effect.mapper.TFamilyTrialMapper;
import com.lfz.quality_effect.model.dto.FamilyTrialDTO;
import com.lfz.quality_effect.model.entity.TFamilyTrial;
import com.lfz.quality_effect.model.vo.*;
import com.lfz.quality_effect.service.TFamilyTrialService;
import com.lfz.quality_effect.utils.ExcelListener;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class TFamilyTrialServiceImpl extends ServiceImpl<TFamilyTrialMapper, TFamilyTrial> implements TFamilyTrialService {

    /**
     * 家事审判——上传收结存
     * @param file
     */
    @Override
    public void uploadFamilyTrial(MultipartFile file) {

        ExcelListener listener = new ExcelListener();
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //传入参数
        ExcelReader excelReader = new ExcelReader(inputStream, null, listener);
        Sheet sheet = new Sheet(1, 2, FamilyTrialDTO.class);

        //读取信息
        excelReader.read(sheet);
        List<Object> objectList =listener.getDatas();
        // 案件类型
        Map<String, String> caseTypeMap = new HashMap<String, String>() {{
            put("婚约财产纠纷", "婚姻类");
            put("离婚纠纷", "婚姻类");
            put("离婚后财产纠纷", "婚姻类");
            put("离婚后损害责任纠纷", "婚姻类");
            put("同居纠纷", "婚姻类");
            put("同居关系析产纠纷", "婚姻类");
            put("抚养纠纷", "抚养类");
            put("变更抚养纠纷", "抚养类");
            put("同居关系子女抚养纠纷", "抚养类");
            put("赡养费纠纷", "赡养类");
            put("赡养纠纷", "赡养类");
            put("继承纠纷", "继承类");
            put("遗嘱继承纠纷", "继承类");
            put("法定继承纠纷", "继承类");
        }};

        // 是否外派
        Map<String, String> courtLocationMap = new HashMap<>();
        courtLocationMap.put("港口法庭", "港口");
        courtLocationMap.put("高家法庭", "高家");
        courtLocationMap.put("接渡法庭", "接渡");
        courtLocationMap.put("立案庭", "非外派");
        courtLocationMap.put("民一庭", "非外派");
        courtLocationMap.put("民二庭", "非外派");
        courtLocationMap.put("双田法庭", "双田");
        courtLocationMap.put("速裁团队", "非外派");
        courtLocationMap.put("行政庭", "非外派");
        courtLocationMap.put("研究室", "非外派");
        courtLocationMap.put("院领导", "非外派");
        courtLocationMap.put("众埠法庭", "众埠");

        ArrayList<TFamilyTrial> tList = new ArrayList<>();
        for (int i = 0; i < objectList.size(); i++) {
            FamilyTrialDTO familyTrialDTO = (FamilyTrialDTO) objectList.get(i);

            String causeAction = familyTrialDTO.getCauseAction();
            // 通过案由筛选出 家事审判类
            String caseType = caseTypeMap.get(causeAction);

            // 属于家事审判类
            if (caseType != null && !caseType.equals("")){

                TFamilyTrial tFamilyTrial = new TFamilyTrial();
                BeanUtils.copyProperties(familyTrialDTO,tFamilyTrial);
                String civiCaseNo = familyTrialDTO.getCiviCaseNo();
                String courtRoom = familyTrialDTO.getCourtRoom();
                // 通过民初案号 截取年份
                String year = civiCaseNo.replaceAll("[^\\d]", "").substring(0,4);
                // 是否外派
                String isSend = courtLocationMap.get(courtRoom);

                tFamilyTrial.setCreateTime(LocalDateTime.now());
                tFamilyTrial.setYear(year);
                tFamilyTrial.setCaseType(caseType);
                tFamilyTrial.setIsSend(isSend);
                tList.add(tFamilyTrial);
            }
        }
        this.saveOrUpdateBatch(tList);
    }

    /**
     * 获取受理趋势
     *
     * @return
     */
    @Override
    public List<AcceptTrendVO> getAcceptTrend(String isSend) {
        return this.getBaseMapper().getAcceptTrend(isSend);
    }

    /**
     * 获取调解撤诉趋势
     * @return
     */
    @Override
    public List<MediateWithdrawVO> getMediateAndWithdrawTrend(String isSend) {
        return this.getBaseMapper().getMediateAndWithdrawTrend(isSend);
    }

    /**
     * 获取案由分布
     * @return
     */
    @Override
    public List<CaseActionVO> getCaseActionDistribution(String isSend) {
        return this.getBaseMapper().getCaseActionDistribution(isSend);
    }

    /**
     * 获取地理分布
     * @return
     */
    @Override
    public List<LocationVO> getLocationDistribution() {
        return this.getBaseMapper().getLocationDistribution();
    }

    @Override
    public List<CaseNumByCourtVO> getTotalCaseNumGroupByCourt(IsThisYearVO isThisYearVO) {
        List<CaseNumByCourtVO> list = new ArrayList<>();
        String isThisYear = isThisYearVO.getIsThisYear();

        if ("true".equals(isThisYear) || "false".equals(isThisYear)) {
            list = "true".equals(isThisYear) ?
                this.getBaseMapper().getTotalCaseNumGroupByCourtThisYear() : this.getBaseMapper().getTotalCaseNumGroupByCourtHistory();
        }

        return list;

    }

}
